<?php


namespace app\index\controller\xilujob;


use app\common\controller\xilujob\Xilujob;
use app\common\model\xilujob\Job;
use app\common\model\xilujob\ResumeActionLog;
use app\common\model\xilujob\JobApply AS JobApplyModel;
use think\Exception;

class JobApply extends Xilujob
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    protected $restrictedActions = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = new JobApplyModel();
    }

    public function index(){
        $this->relationSearch = true;
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $lists = $this->model
                ->with([
                    'user'=>function($query){$query->withField('id,nickname,avatar');},
                    'resume',
                    'job'=>function($query){$query->withField('id,job_name');},
                ])
                ->where('company_id', $this->company->id)
                ->where($where)
                ->order('createtime','desc')
                ->paginate($limit)->each(function ($row){
                    $row->resume->relationQuery([ 'personal','works','summary']);
                    $row->has_actino_log = ResumeActionLog::hasLog($row->company_id, $row->user_id, $row->resume_id);
                });
            $result = array("total" => $lists->total(), "rows" => $lists->items());

            return json($result);
        }
        $this->assignconfig('jobs', Job::where('publisher_id',$this->company->id)->column('id,job_name as name'));
        $this->assign('title','应聘简历');
        return $this->view->fetch();
    }


    /**
     * 企业拒绝简历投递
     * @param $id
     */
    public function reject_apply($ids) {

        $applys = $this->model->where('id','in',$ids)->select();
        if(!$applys){
            $this->error('为找到投递记录');
        }
        try {
            foreach ($applys as $apply){
                if($apply->company_id!=$this->company->id) {
                    $this->error(__('no record'));
                }
                $ret = JobApplyModel::editApply($apply['id'], JobApplyModel::STATUS_APPLY_REJECTED);
            }

        }catch (Exception $e){
            $this->error($e->getMessage());
        }

        $this->success('', '', $ret);
    }
}